package com.qf.dao.impl;

import com.qf.dao.CartDao;
import com.qf.pojo.Cart;
import com.qf.pojo.Goods;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class CartDaoImpl implements CartDao {
    @Override
    public Cart findByUidAndGid(int uid, int pid) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select id,pid,Num,money from tb_cart where id=? and pid=? ";
        try {
            Cart cart= qf.query(sql,new BeanHandler<>(Cart.class),uid,pid);
            return cart;
        } catch (SQLException e) {

            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void insert(Cart cart) {

            QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
            String sql="INSERT INTO  `tb_cart` (`id`, `pid`, `Num`, `money`)   VALUES (?, ?, ?, ?)  ";
            try {
                 qf.update(sql,cart.getId(),cart.getPid(),cart.getNum(),cart.getMoney());

            } catch (SQLException e) {

                e.printStackTrace();
                System.out.println("新增失败");
            }

    }

    @Override
    public void updateCart(Cart cart) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "UPDATE `tb_cart` SET Num=?, money=? where  (id=?) AND (pid=?) ";
        try {
            qf.update(sql,cart.getNum(),cart.getMoney(),cart.getId(),cart.getPid());

        } catch (SQLException e) {

            e.printStackTrace();
            System.out.println("修改失败");
        }
    }
}
